﻿// Decompiled with JetBrains decompiler
// Type: 我的小工具.frmCreateSite
// Assembly: 开发辅助工具, Version=1.0.0.1, Culture=neutral, PublicKeyToken=null
// MVID: F78E5980-5E94-4E46-ABDF-DC8C6CBF52AA
// Assembly location: K:\Tools\工具\开发辅助工具庆六一版\开发辅助工具2015-06-01\开发辅助工具.exe

using Microsoft.Web.Administration;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.DirectoryServices;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace 我的小工具
{
  public class frmCreateSite : Form
  {
    private Dictionary<string, SiteObj> dicSiteObj = new Dictionary<string, SiteObj>();
    private IContainer components = (IContainer) null;
    private Button btn_Next;
    private Button btnCancel;
    private Label label4;
    private TextBox txtPort;
    private Button btn_Pre;
    private Button btn_CreateSite;
    private GroupBox gBox_SiteInfo;
    private Label label9;
    private TextBox txtSitePath;
    private Label label16;
    private TextBox txtSiteName;
    private Label label11;
    private ComboBox txtAss;
    private Label label10;
    private Label label12;
    private ComboBox cboReg;
    private Label label17;
    private TextBox txtSourceAdress;
    private TextBox txtDBName;
    private Label label15;
    private ComboBox txtServerName2;
    private Label label14;
    private ComboBox cboUserName;
    private Label label13;
    private ComboBox cboPwd;
    private GroupBox groupBox1;
    private GroupBox gBox_Source;
    private Label lbl_Err;

    public frmCreateSite()
    {
      this.InitializeComponent();
    }

    private void CreateWebSite()
    {
      int iisMajorVersion = IISHelper.GetIISMajorVersion();
      string siteExplain = this.txtSiteName.Text.Trim();
      switch (iisMajorVersion)
      {
        case 6:
          this.ShowInfo("IIS6暂不支持,待开发中!");
          break;
        case 7:
          ServerManager serverManager = new ServerManager();
          serverManager.ApplicationPools[siteExplain].ManagedRuntimeVersion = "v2.0";
          serverManager.ApplicationPools[siteExplain].ManagedPipelineMode = ManagedPipelineMode.Classic;
          serverManager.CommitChanges();
          break;
      }
      try
      {
        frmCreateSite.CreateWebSite(this.txtPort.Text.Trim(), this.txtSitePath.Text.Trim(), siteExplain, "");
        int num = (int) MessageBox.Show(siteExplain + "程序池托管管道模式：经典模式；运行的NET版本为:v2.0");
        this.Close();
      }
      catch (Exception ex)
      {
        int num = (int) MessageBox.Show(ex.Message);
      }
    }

    public static int CreateWebSite(string port, string ppath, string siteExplain, string defaultDoc)
    {
      DirectoryEntry directoryEntry1 = new DirectoryEntry("IIS://localhost/w3svc");
      int num = 1;
      foreach (DirectoryEntry child in directoryEntry1.Children)
      {
        if (child.SchemaClassName == "IIsWebServer")
        {
          int int32 = Convert.ToInt32(child.Name);
          if (int32 >= num)
            num = int32 + 1;
        }
      }
      object[] objArray = new object[2]
      {
        (object) "IIsWebServer",
        (object) num
      };
      DirectoryEntry directoryEntry2 = (DirectoryEntry) directoryEntry1.Invoke("Create", objArray);
      directoryEntry2.Properties["KeyType"][0] = (object) "IIsWebServer";
      directoryEntry2.Properties["ServerComment"][0] = (object) siteExplain;
      directoryEntry2.Properties["ServerState"][0] = (object) 2;
      directoryEntry2.Properties["ServerSize"][0] = (object) 1;
      directoryEntry2.Properties["ServerBindings"].Add((object) (":" + port + ":"));
      directoryEntry2.CommitChanges();
      directoryEntry1.CommitChanges();
      DirectoryEntry directoryEntry3 = directoryEntry2.Children.Add("Root", "IIsWebVirtualDir");
      directoryEntry3.Invoke("AppCreate", (object) true);
      directoryEntry3.Properties["path"][0] = (object) ppath;
      directoryEntry3.Properties["EnableDirBrowsing"][0] = (object) false;
      directoryEntry3.Properties["AuthAnonymous"][0] = (object) true;
      directoryEntry3.Properties["AccessExecute"][0] = (object) false;
      directoryEntry3.Properties["AccessRead"][0] = (object) true;
      directoryEntry3.Properties["AccessWrite"][0] = (object) false;
      directoryEntry3.Properties["AccessScript"][0] = (object) true;
      directoryEntry3.Properties["AccessSource"][0] = (object) false;
      directoryEntry3.Properties["FrontPageWeb"][0] = (object) false;
      directoryEntry3.Properties["KeyType"][0] = (object) "IIsWebVirtualDir";
      directoryEntry3.Properties["AppFriendlyName"][0] = (object) siteExplain;
      directoryEntry3.Properties["AppIsolated"][0] = (object) 2;
      string runtimeDirectory = RuntimeEnvironment.GetRuntimeDirectory();
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.AppendLine(".aspx," + Path.Combine(runtimeDirectory, "aspnet_isapi.dll") + ",1,GET,HEAD,POST,DEBUG");
      stringBuilder.AppendLine(".ashx," + Path.Combine(runtimeDirectory, "aspnet_isapi.dll") + ",1,GET,HEAD,POST,DEBUG");
      stringBuilder.AppendLine(".asax," + Path.Combine(runtimeDirectory, "aspnet_isapi.dll") + ",5,GET,HEAD,POST,DEBUG");
      stringBuilder.AppendLine(".config," + Path.Combine(runtimeDirectory, "aspnet_isapi.dll") + ",5,GET,HEAD,POST,DEBUG");
      stringBuilder.AppendLine(".ascx," + Path.Combine(runtimeDirectory, "aspnet_isapi.dll") + ",5,GET,HEAD,POST,DEBUG");
      directoryEntry3.Properties["ScriptMaps"].Value = (object) stringBuilder.ToString();
      directoryEntry3.CommitChanges();
      directoryEntry2.CommitChanges();
      directoryEntry3.Close();
      directoryEntry2.Close();
      directoryEntry1.CommitChanges();
      directoryEntry2.Invoke("Start", (object[]) null);
      return num;
    }

    private bool ValidSite()
    {
      if (this.txtSiteName.Text.Trim() == "")
      {
        this.ShowInfo("请输入站点名称!");
        this.txtSiteName.Focus();
        this.txtSiteName.SelectAll();
        return false;
      }
      if (this.txtSitePath.Text.Trim() == "")
      {
        this.ShowInfo("请输入站点目录!");
        this.txtSitePath.Focus();
        this.txtSitePath.SelectAll();
        return false;
      }
      if (this.txtAss.Text.Trim() == "")
      {
        this.ShowInfo("应用程序池非空!");
        this.txtAss.Focus();
        this.txtAss.SelectAll();
        return false;
      }
      if (this.txtPort.Text.Trim() == "")
      {
        this.ShowInfo("端口非空!");
        this.txtPort.Focus();
        this.txtPort.SelectAll();
        return false;
      }
      if (!this.IsSitePort(this.txtPort.Text.Trim()))
        return true;
      this.ShowInfo("端口已存在，请调整!");
      this.txtPort.Focus();
      this.txtPort.SelectAll();
      return false;
    }

    private bool IsAppPoolName(string AppPoolName)
    {
      bool flag = false;
      foreach (DirectoryEntry child in new DirectoryEntry("IIS://localhost/W3SVC/AppPools").Children)
      {
        if (child.Name.Equals(AppPoolName))
          flag = true;
      }
      return flag;
    }

    private bool DeleteAppPool(string AppPoolName)
    {
      bool flag = false;
      foreach (DirectoryEntry child in new DirectoryEntry("IIS://localhost/W3SVC/AppPools").Children)
      {
        if (child.Name.Equals(AppPoolName))
        {
          try
          {
            child.DeleteTree();
            flag = true;
          }
          catch
          {
            flag = false;
          }
        }
      }
      return flag;
    }

    public static bool DeleteSite(int siteidon)
    {
      DirectoryEntry directoryEntry = new DirectoryEntry("IIS://localhost/W3SVC/" + siteidon.ToString());
      directoryEntry.RefreshCache();
      directoryEntry.DeleteTree();
      directoryEntry.Close();
      return true;
    }

    private bool IsSitePort(string SitePort)
    {
      this.dicSiteObj = IISHelper.GetSiteNames();
      foreach (KeyValuePair<string, SiteObj> keyValuePair in this.dicSiteObj)
      {
        if (keyValuePair.Value.Port == SitePort)
          return true;
      }
      return false;
    }

    private void ShowInfo(string errInfo)
    {
      this.lbl_Err.Visible = true;
      this.lbl_Err.Text = errInfo;
    }

    private void HideInfo()
    {
      this.lbl_Err.Visible = false;
      this.lbl_Err.Text = "";
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void btn_Next_Click(object sender, EventArgs e)
    {
      if (!this.ValidSite())
        return;
      this.InitBtnState("next");
    }

    private void btn_CreateSite_Click(object sender, EventArgs e)
    {
      this.CreateWebSite();
    }

    private void frmCreateSite_Load(object sender, EventArgs e)
    {
      this.InitBtnState("new");
    }

    private void btn_Pre_Click(object sender, EventArgs e)
    {
      this.InitBtnState("pre");
    }

    private void txtSiteName_TextChanged(object sender, EventArgs e)
    {
      this.txtAss.Text = this.txtSiteName.Text;
    }

    private void InitBtnState(string stype)
    {
      switch (stype)
      {
        case "next":
          this.gBox_SiteInfo.Visible = false;
          this.gBox_Source.Visible = true;
          this.btn_CreateSite.Visible = true;
          this.btn_Pre.Visible = true;
          this.btn_Next.Visible = false;
          break;
        default:
          this.gBox_SiteInfo.Visible = true;
          this.gBox_Source.Visible = false;
          this.btn_CreateSite.Visible = false;
          this.btn_Pre.Visible = false;
          this.btn_Next.Visible = true;
          break;
      }
    }

    private void txtPort_MouseLeave(object sender, EventArgs e)
    {
      if (this.IsSitePort(this.txtPort.Text.Trim()))
        this.ShowInfo("端口已存在，请调整!");
      else
        this.HideInfo();
    }

    private void txtPort_TextChanged(object sender, EventArgs e)
    {
      if (this.IsSitePort(this.txtPort.Text.Trim()))
        this.ShowInfo("端口已存在，请调整!");
      else
        this.HideInfo();
    }

    protected override void Dispose(bool disposing)
    {
      if (disposing && this.components != null)
        this.components.Dispose();
      base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
      this.btn_Next = new Button();
      this.btnCancel = new Button();
      this.txtPort = new TextBox();
      this.label4 = new Label();
      this.btn_Pre = new Button();
      this.btn_CreateSite = new Button();
      this.gBox_SiteInfo = new GroupBox();
      this.lbl_Err = new Label();
      this.label9 = new Label();
      this.txtSitePath = new TextBox();
      this.label16 = new Label();
      this.txtSiteName = new TextBox();
      this.label11 = new Label();
      this.txtAss = new ComboBox();
      this.label10 = new Label();
      this.label12 = new Label();
      this.cboReg = new ComboBox();
      this.label17 = new Label();
      this.txtSourceAdress = new TextBox();
      this.txtDBName = new TextBox();
      this.label15 = new Label();
      this.txtServerName2 = new ComboBox();
      this.label14 = new Label();
      this.cboUserName = new ComboBox();
      this.label13 = new Label();
      this.cboPwd = new ComboBox();
      this.groupBox1 = new GroupBox();
      this.gBox_Source = new GroupBox();
      this.gBox_SiteInfo.SuspendLayout();
      this.groupBox1.SuspendLayout();
      this.gBox_Source.SuspendLayout();
      this.SuspendLayout();
      this.btn_Next.Location = new Point(490, 20);
      this.btn_Next.Name = "btn_Next";
      this.btn_Next.Size = new Size(75, 23);
      this.btn_Next.TabIndex = 1;
      this.btn_Next.Text = "下一步";
      this.btn_Next.UseVisualStyleBackColor = true;
      this.btn_Next.Click += new EventHandler(this.btn_Next_Click);
      this.btnCancel.Location = new Point(571, 20);
      this.btnCancel.Name = "btnCancel";
      this.btnCancel.Size = new Size(75, 23);
      this.btnCancel.TabIndex = 2;
      this.btnCancel.Text = "取消";
      this.btnCancel.UseVisualStyleBackColor = true;
      this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
      this.txtPort.Location = new Point(409, 74);
      this.txtPort.Name = "txtPort";
      this.txtPort.Size = new Size(215, 21);
      this.txtPort.TabIndex = 3;
      this.txtPort.TextChanged += new EventHandler(this.txtPort_TextChanged);
      this.txtPort.MouseLeave += new EventHandler(this.txtPort_MouseLeave);
      this.label4.AutoSize = true;
      this.label4.Location = new Point(362, 77);
      this.label4.Name = "label4";
      this.label4.Size = new Size(41, 12);
      this.label4.TabIndex = 3;
      this.label4.Text = "端口：";
      this.btn_Pre.Location = new Point(409, 20);
      this.btn_Pre.Name = "btn_Pre";
      this.btn_Pre.Size = new Size(75, 23);
      this.btn_Pre.TabIndex = 0;
      this.btn_Pre.Text = "上一步";
      this.btn_Pre.UseVisualStyleBackColor = true;
      this.btn_Pre.Click += new EventHandler(this.btn_Pre_Click);
      this.btn_CreateSite.Location = new Point(490, 20);
      this.btn_CreateSite.Name = "btn_CreateSite";
      this.btn_CreateSite.Size = new Size(75, 23);
      this.btn_CreateSite.TabIndex = 3;
      this.btn_CreateSite.Text = "创建站点";
      this.btn_CreateSite.UseVisualStyleBackColor = true;
      this.btn_CreateSite.Click += new EventHandler(this.btn_CreateSite_Click);
      this.gBox_SiteInfo.Controls.Add((Control) this.lbl_Err);
      this.gBox_SiteInfo.Controls.Add((Control) this.label9);
      this.gBox_SiteInfo.Controls.Add((Control) this.txtSitePath);
      this.gBox_SiteInfo.Controls.Add((Control) this.label16);
      this.gBox_SiteInfo.Controls.Add((Control) this.txtSiteName);
      this.gBox_SiteInfo.Controls.Add((Control) this.txtPort);
      this.gBox_SiteInfo.Controls.Add((Control) this.label4);
      this.gBox_SiteInfo.Controls.Add((Control) this.label11);
      this.gBox_SiteInfo.Controls.Add((Control) this.txtAss);
      this.gBox_SiteInfo.Dock = DockStyle.Top;
      this.gBox_SiteInfo.Location = new Point(0, 0);
      this.gBox_SiteInfo.Name = "gBox_SiteInfo";
      this.gBox_SiteInfo.Size = new Size(646, 128);
      this.gBox_SiteInfo.TabIndex = 43;
      this.gBox_SiteInfo.TabStop = false;
      this.gBox_SiteInfo.Text = "配置信息";
      this.lbl_Err.AutoSize = true;
      this.lbl_Err.ForeColor = Color.Red;
      this.lbl_Err.Location = new Point(316, 23);
      this.lbl_Err.Name = "lbl_Err";
      this.lbl_Err.Size = new Size(53, 12);
      this.lbl_Err.TabIndex = 7;
      this.lbl_Err.Text = "提示信息";
      this.lbl_Err.Visible = false;
      this.label9.AutoSize = true;
      this.label9.Location = new Point(23, 51);
      this.label9.Name = "label9";
      this.label9.Size = new Size(65, 12);
      this.label9.TabIndex = 3;
      this.label9.Text = "站点目录：";
      this.txtSitePath.Cursor = Cursors.Hand;
      this.txtSitePath.ForeColor = Color.Blue;
      this.txtSitePath.Location = new Point(92, 47);
      this.txtSitePath.Name = "txtSitePath";
      this.txtSitePath.Size = new Size(532, 21);
      this.txtSitePath.TabIndex = 1;
      this.label16.AutoSize = true;
      this.label16.Location = new Point(23, 24);
      this.label16.Name = "label16";
      this.label16.Size = new Size(65, 12);
      this.label16.TabIndex = 3;
      this.label16.Text = "站点名称：";
      this.txtSiteName.Location = new Point(92, 20);
      this.txtSiteName.Name = "txtSiteName";
      this.txtSiteName.Size = new Size(208, 21);
      this.txtSiteName.TabIndex = 0;
      this.txtSiteName.TextChanged += new EventHandler(this.txtSiteName_TextChanged);
      this.label11.AutoSize = true;
      this.label11.Location = new Point(23, 78);
      this.label11.Name = "label11";
      this.label11.Size = new Size(65, 12);
      this.label11.TabIndex = 3;
      this.label11.Text = "应用程序：";
      this.txtAss.FormattingEnabled = true;
      this.txtAss.Location = new Point(92, 74);
      this.txtAss.Name = "txtAss";
      this.txtAss.Size = new Size(208, 20);
      this.txtAss.TabIndex = 2;
      this.label10.AutoSize = true;
      this.label10.Location = new Point(22, 34);
      this.label10.Name = "label10";
      this.label10.Size = new Size(65, 12);
      this.label10.TabIndex = 3;
      this.label10.Text = "源码地址：";
      this.label12.AutoSize = true;
      this.label12.Location = new Point(36, 62);
      this.label12.Name = "label12";
      this.label12.Size = new Size(53, 12);
      this.label12.TabIndex = 5;
      this.label12.Text = "注册表：";
      this.cboReg.FormattingEnabled = true;
      this.cboReg.Location = new Point(93, 58);
      this.cboReg.Name = "cboReg";
      this.cboReg.Size = new Size(208, 20);
      this.cboReg.TabIndex = 1;
      this.label17.AutoSize = true;
      this.label17.Location = new Point(335, 115);
      this.label17.Name = "label17";
      this.label17.Size = new Size(77, 12);
      this.label17.TabIndex = 21;
      this.label17.Text = "SaPassword：";
      this.txtSourceAdress.Location = new Point(92, 31);
      this.txtSourceAdress.Name = "txtSourceAdress";
      this.txtSourceAdress.Size = new Size(532, 21);
      this.txtSourceAdress.TabIndex = 0;
      this.txtDBName.Location = new Point(417, 84);
      this.txtDBName.Name = "txtDBName";
      this.txtDBName.Size = new Size(208, 21);
      this.txtDBName.TabIndex = 4;
      this.label15.AutoSize = true;
      this.label15.Location = new Point(23, 115);
      this.label15.Name = "label15";
      this.label15.Size = new Size(65, 12);
      this.label15.TabIndex = 19;
      this.label15.Text = "UserName：";
      this.txtServerName2.Items.AddRange(new object[5]
      {
        (object) "10.5.10.75\\SQL2012_LY",
        (object) "10.5.10.75\\SQL2008R2_LY",
        (object) "10.5.10.75\\SQL2008_LY",
        (object) "10.5.10.75\\SQL2005_LY",
        (object) "10.5.10.75\\SQL2000"
      });
      this.txtServerName2.Location = new Point(92, 84);
      this.txtServerName2.Name = "txtServerName2";
      this.txtServerName2.Size = new Size(208, 20);
      this.txtServerName2.TabIndex = 2;
      this.txtServerName2.Text = "10.5.10.75\\sql2008R2_LY";
      this.label14.AutoSize = true;
      this.label14.Location = new Point(359, 88);
      this.label14.Name = "label14";
      this.label14.Size = new Size(53, 12);
      this.label14.TabIndex = 17;
      this.label14.Text = "DBName：";
      this.cboUserName.DisplayMember = "team_ly";
      this.cboUserName.FormattingEnabled = true;
      this.cboUserName.Items.AddRange(new object[3]
      {
        (object) "sa",
        (object) "team_ly",
        (object) "team_cy"
      });
      this.cboUserName.Location = new Point(92, 111);
      this.cboUserName.Name = "cboUserName";
      this.cboUserName.Size = new Size(208, 20);
      this.cboUserName.TabIndex = 3;
      this.cboUserName.Text = "team_ly";
      this.label13.AutoSize = true;
      this.label13.Location = new Point(11, 88);
      this.label13.Name = "label13";
      this.label13.Size = new Size(77, 12);
      this.label13.TabIndex = 15;
      this.label13.Text = "ServerName：";
      this.cboPwd.FormattingEnabled = true;
      this.cboPwd.Items.AddRange(new object[1]
      {
        (object) "95938"
      });
      this.cboPwd.Location = new Point(417, 111);
      this.cboPwd.Name = "cboPwd";
      this.cboPwd.Size = new Size(208, 20);
      this.cboPwd.TabIndex = 5;
      this.cboPwd.Text = "95938";
      this.groupBox1.Controls.Add((Control) this.btn_Next);
      this.groupBox1.Controls.Add((Control) this.btnCancel);
      this.groupBox1.Controls.Add((Control) this.btn_Pre);
      this.groupBox1.Controls.Add((Control) this.btn_CreateSite);
      this.groupBox1.Dock = DockStyle.Bottom;
      this.groupBox1.Location = new Point(0, 300);
      this.groupBox1.Name = "groupBox1";
      this.groupBox1.Size = new Size(646, 58);
      this.groupBox1.TabIndex = 44;
      this.groupBox1.TabStop = false;
      this.gBox_Source.Controls.Add((Control) this.txtServerName2);
      this.gBox_Source.Controls.Add((Control) this.cboPwd);
      this.gBox_Source.Controls.Add((Control) this.label10);
      this.gBox_Source.Controls.Add((Control) this.label13);
      this.gBox_Source.Controls.Add((Control) this.cboUserName);
      this.gBox_Source.Controls.Add((Control) this.label14);
      this.gBox_Source.Controls.Add((Control) this.label15);
      this.gBox_Source.Controls.Add((Control) this.txtDBName);
      this.gBox_Source.Controls.Add((Control) this.txtSourceAdress);
      this.gBox_Source.Controls.Add((Control) this.label12);
      this.gBox_Source.Controls.Add((Control) this.label17);
      this.gBox_Source.Controls.Add((Control) this.cboReg);
      this.gBox_Source.Dock = DockStyle.Fill;
      this.gBox_Source.Location = new Point(0, 128);
      this.gBox_Source.Name = "gBox_Source";
      this.gBox_Source.Size = new Size(646, 172);
      this.gBox_Source.TabIndex = 45;
      this.gBox_Source.TabStop = false;
      this.gBox_Source.Text = "数据源信息";
      this.AutoScaleDimensions = new SizeF(6f, 12f);
      this.AutoScaleMode = AutoScaleMode.Font;
      this.ClientSize = new Size(646, 358);
      this.Controls.Add((Control) this.gBox_Source);
      this.Controls.Add((Control) this.groupBox1);
      this.Controls.Add((Control) this.gBox_SiteInfo);
      this.Name = "frmCreateSite";
      this.StartPosition = FormStartPosition.CenterScreen;
      this.Text = "新建站点信息";
      this.Load += new EventHandler(this.frmCreateSite_Load);
      this.gBox_SiteInfo.ResumeLayout(false);
      this.gBox_SiteInfo.PerformLayout();
      this.groupBox1.ResumeLayout(false);
      this.gBox_Source.ResumeLayout(false);
      this.gBox_Source.PerformLayout();
      this.ResumeLayout(false);
    }
  }
}
